<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>城市天气查询</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .weather-app {
            background: #ffffff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        .weather-app input {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .weather-app button {
            padding: 10px 15px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .weather-app button:hover {
            background-color: #0056b3;
        }

        .weather-info {
            margin-top: 15px;
        }

        .weather-info p {
            margin: 5px 0;
        }
    </style>
</head>
<body>
    <div id="app" class="weather-app">
        <h1>天气查询</h1>
        <input v-model="city" @keyup.enter="getWeather" placeholder="请输入城市名">
        <button @click="getWeather">查询天气</button>

        <div class="weather-info" v-if="weather">
            <p><strong>城市：</strong>{{ weather.city }}</p>
            <p><strong>温度：</strong>{{ weather.temperature }}℃</p>
            <p><strong>天气：</strong>{{ weather.weather }}</p>
            <p><strong>风向：</strong>{{ weather.wind }}</p>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                city: '',
                weather: null
            },
            methods: {
                async getWeather() {
                    if (!this.city) {
                        alert('请输入城市名称');
                        return;
                    }

                    const key = 'e7b0f40118eb4ed19d9ee25a9e8072dc'; // 替换为您的和风天气API密钥
                    const locationUrl = `https://geoapi.qweather.com/v2/city/lookup?location=${this.city}&key=${key}`;

                    try {
                        // 获取城市的 location ID
                        const locationResponse = await axios.get(locationUrl);
                        const locationData = locationResponse.data;

                        if (!locationData || locationData.code !== "200" || !locationData.location || locationData.location.length === 0) {
                            alert('查询失败，请检查城市名称是否正确');
                            return;
                        }

                        const locationId = locationData.location[0].id;

                        // 查询天气信息
                        const weatherUrl = `https://devapi.qweather.com/v7/weather/now?location=${locationId}&key=${key}`;
                        const weatherResponse = await axios.get(weatherUrl);
                        const weatherData = weatherResponse.data;

                        if (weatherData && weatherData.code === "200") {
                            this.weather = {
                                city: weatherData.now.obsTime.split("T")[0] + "时间",
                                temperature: weatherData.now.temp,
                                weather: weatherData.now.text,
                                wind: weatherData.now.windDir
                            };
                        } else {
                            alert('无法获取天气信息，请稍后再试');
                        }
                    } catch (error) {
                        console.error(error);
                        alert('无法获取天气信息，请稍后再试');
                    }
                }
            }
        });
    </script>
</body>
</html>
